home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / samples / tuidemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  6.1 KB  |  240 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. /********************************* tuidemo.c ********************************/
  20. /*
  21.  * File   : tuidemo.c
  22.  * Author : P.J. Kunst  (kunst@prl.philips.nl)
  23.  * Date   : 25-02-93
  24.  * Version: 1.02
  25.  *
  26.  * Purpose: This program demonstrates the use of the 'curses' library
  27.  *          for the creation of (simple) menu-operated programs.
  28.  *          In the PD-Curses version, use is made of colors for the
  29.  *          highlighting of subwindows (title bar, status bar etc).
  30.  *          The program was tested using DJGPP 1.09 ('GO32') and
  31.  *          Turbo C (2.0), as well as on a UNIX machine (HP-UX 8.07).
  32.  *
  33.  * Acknowledgement: some ideas were borrowed from Mark Hessling's
  34.  *                  version of the 'testcurs' program.
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "tui.h"
  41.  
  42. #ifdef PDCDEBUG
  43. char *rcsid_tuidemo  = "$Id$";
  44. #endif
  45.  
  46. #if defined(XCURSES)
  47.     char *XCursesProgramName = "tuidemo";
  48. #endif
  49.  
  50. #define FNAME   "tui.c"    /* change this if source at other location */
  51.  
  52.  
  53. /**************************** strings entry box ***************************/
  54.  
  55. void address (void)
  56. {
  57.   char *fieldname[] = {"Name","Street","City","State","Country",(char*)0};
  58.   char *fieldbuf[5];
  59.   WINDOW *wbody = bodywin();
  60.   int i, field = 50;
  61.  
  62.   for (i=0; i<5; i++) fieldbuf[i] = (char *) calloc (field+1, sizeof(char));
  63.   if (getstrings (fieldname, fieldbuf, field) != KEY_ESC)
  64.   {
  65.     for (i=0; fieldname[i]; i++)
  66.       wprintw (wbody, "%10s : %s\n", fieldname[i], fieldbuf[i]);
  67.     wrefresh (wbody);
  68.   }
  69.   for (i=0; i<5; i++) free (fieldbuf[i]);
  70. }
  71.  
  72. /**************************** string entry box ****************************/
  73.  
  74. char *getfname (char *desc, char *fname, int field)
  75. {
  76.   char *fieldname[2] = { 0, 0 };
  77.   char *fieldbuf[1];
  78.  
  79.   fieldname[0] = desc; fieldbuf[0] = fname;
  80.   return (getstrings (fieldname, fieldbuf, field) == KEY_ESC) ? NULL : fname;
  81. }
  82.  
  83. /**************************** a very simple file browser ******************/
  84.  
  85. void showfile (char *fname)
  86. {
  87.   int i, bh = bodylen();
  88.   FILE *fp;
  89.   char buf[MAXSTRLEN];
  90.   bool ateof = FALSE;
  91.  
  92.   statusmsg ("FileBrowser: Hit key to continue, Q to quit");
  93.  
  94.   if ((fp = fopen (fname, "r")) != NULL)   /* file available ? */
  95.   {
  96.     while (!ateof)
  97.     {
  98.       clsbody ();
  99.       for (i=0; i<bh-1 && !ateof; i++)
  100.       {
  101.         buf[0] = '\0';
  102.         fgets (buf, MAXSTRLEN, fp);
  103.         if (strlen(buf)) bodymsg (buf); else ateof = TRUE;
  104.       }
  105.       switch (waitforkey())
  106.       {
  107.         case 'Q':
  108.         case 'q':
  109.         case 0x1b: /* ESCAPE */
  110.           ateof = TRUE;
  111.           break;
  112.  
  113.         default:
  114.           break;
  115.       }
  116.     }
  117.     fclose (fp);
  118.   }
  119.   else
  120.   {
  121.     sprintf (buf, "ERROR: file '%s' not found", fname);
  122.     errormsg (buf);
  123.   }
  124. }
  125.  
  126. /***************************** forward declarations ***********************/
  127.  
  128. void sub0(), sub1(), sub2(), sub3();
  129. void func1(), func2();
  130. void subfunc1(), subfunc2();
  131. void subsub();
  132.  
  133. /***************************** menus initialization ***********************/
  134.  
  135. menu MainMenu[] =
  136. {
  137.   "Asub",     sub0,     "Go inside first submenu",
  138.   "Bsub",     sub1,     "Go inside second submenu",
  139.   "Csub",     sub2,     "Go inside third submenu",
  140.   "Dsub",     sub3,     "Go inside fourth submenu",
  141.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  142. };
  143.  
  144. menu SubMenu0[] =
  145. {
  146.   "Exit",     DoExit,     "Terminate program",
  147.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  148. };
  149.  
  150. menu SubMenu1[] =
  151. {
  152.   "OneBeep",  func1,    "Sound one beep",
  153.   "TwoBeeps", func2,    "Sound two beeps",
  154.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  155. };
  156.  
  157. menu SubMenu2[] =
  158. {
  159.   "Browse",   subfunc1, "Source file lister",
  160.   "Input",    subfunc2, "Interactive file lister",
  161.   "Address",  address,  "Get address data",
  162.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  163. };
  164.  
  165. menu SubMenu3[] =
  166. {
  167.   "SubSub",   subsub,   "Go inside sub-submenu",
  168.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  169. };
  170.  
  171. /***************************** main menu functions ************************/
  172.  
  173. void sub0 (void)
  174. {
  175.   domenu (SubMenu0);
  176. }
  177.  
  178. void sub1 (void)
  179. {
  180.   domenu (SubMenu1);
  181. }
  182.  
  183. void sub2 (void)
  184. {
  185.   domenu (SubMenu2);
  186. }
  187.  
  188. void sub3 (void)
  189. {
  190.   domenu (SubMenu3);
  191. }
  192.  
  193. /***************************** submenu1 functions *************************/
  194.  
  195. void func1 (void)
  196. {
  197.   beep ();
  198.   bodymsg ("One beep! ");
  199. }
  200.  
  201. void func2 (void)
  202. {
  203.   beep ();
  204.   bodymsg ("Two beeps! ");
  205.   beep ();
  206. }
  207.  
  208. /***************************** submenu2 functions *************************/
  209.  
  210. void subfunc1 (void)
  211. {
  212.   showfile (FNAME);
  213. }
  214.  
  215. void subfunc2 (void)
  216. {
  217.   char fname[MAXSTRLEN];
  218.  
  219.   strcpy (fname, FNAME);
  220.   if (getfname ("File to browse:", fname, 50)) showfile (fname);
  221. }
  222.  
  223. /***************************** submenu3 functions *************************/
  224.  
  225. void subsub (void)
  226. {
  227.   domenu (SubMenu2);
  228. }
  229.  
  230. /***************************** start main menu  ***************************/
  231.  
  232. int main ()
  233. {
  234.   startmenu (MainMenu, "TUI - 'textual user interface' demonstration program");
  235.  
  236.   return 0;
  237. }
  238. /********************************* tuidemo.c ********************************/
  239.